package com.young.mylib.view.listPopupWindow;

import android.content.Context;

import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.young.mylib.R;
import com.young.mylib.view.adapter.SimpleListPopupViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jack on 16/5/6.
 */
public class SimpleListPopupWindow extends PopupWindow {
    private Context mContext = null;
    private List<String> mItemList = null;
    private OnItemClickListener mOnItemClickListener = null;
    private SimpleListPopupViewAdapter adapter;

    public interface OnItemClickListener {
        void onItemClick(String str);
    }

    public SimpleListPopupWindow(Context context) {
        super(context);
        mContext = context;
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        setFocusable(true);
    }

    public void setItemList(List<String> itemList) {
        mItemList = itemList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    protected void createContentView(List<String> itemList) {
        if (itemList == null) {
            return;
        }
        adapter = new SimpleListPopupViewAdapter(itemList);
        adapter.setOnItemSelectedListener(new SimpleListPopupViewAdapter.onItemSelectedListener() {
            @Override
            public void onItemSelected(String string) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(string);
                }
            }
        });
        View view = View.inflate(mContext, R.layout.layout_popup_view, null);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rcvRmenuList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

       // recyclerView.addItemDecoration(new EdgeVerticalDividerItemDecoration(mContext, R.drawable.layers_divider_menu));
        recyclerView.setAdapter(adapter);
        setContentView(view);
    }

    public void update(ArrayList<String> itemList) {
        if (adapter != null) {
            adapter.updateData(itemList);
        }
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        if (mItemList != null) {
            createContentView(mItemList);
        }
        super.showAsDropDown(anchor, xoff, yoff, gravity);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (mItemList != null) {
            createContentView(mItemList);
        }
        super.showAsDropDown(anchor, -300, 0);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (mItemList != null) {
            createContentView(mItemList);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

}
